<script>
	// #ifdef APP-PLUS
	import {
		registerRequestPermissionTipsListener,
		unregisterRequestPermissionTipsListener,
		setRequestPermissionTips
	} from "@/uni_modules/uni-registerRequestPermissionTips"
	var PermissionTips = {
		"android.permission.ACCESS_FINE_LOCATION": "<h4 style=\"font-size:40px;\">定位权限</h4><font color=#cccccc>获取您的当前位置信息，用于提供定位服务</font>",
		"android.permission.CALL_PHONE": "<h4 style=\"font-size:40px;\">拨打电话权限</h4><font color=#cccccc>用于应用程序唤起手机电话，拨打电话，请注意不会获取任何信息</font>",
		"android.permission.ACCESS_COARSE_LOCATION": "<h4 style=\"font-size:40px;\">定位权限</h4><font color=#cccccc>获取您的当前位置信息，用于提供定位服务</font>",
		"android.permission.CAMERA": "<h4 style=\"font-size:40px;\">摄像头权限</h4><font color=#cccccc>用于访问您的设备摄像头，以便拍摄照片，提供实时图像功能</font>"
	}//CALL_PHONE打电话权限，ACCESS_FINE_LOCATION定位权限
	// #endif
	export default {
		data() {
			return {
				downloadNum: null,
				showdownLine: false,
				iskeepIndex: 1,
				zygx: ''
			}
		},
		onLaunch: function() {
			// console.log('App Launch')
			var that = this
			uni.getSystemInfo({
				success: function(res) {
					// console.log(res)
					var type = false
					if (res.host || res.platform == 'devtools') {
						// that.init()
						that.iskeppLogin()
					} else {
						that.isDate()
						that.iskeppLogin()
						type = true
					}
					uni.setStorageSync('platform', type);
				}
			});

			// #ifdef APP-PLUS
			plus.push.getClientInfoAsync((info) => {
				let cid = info["clientid"];
				uni.setStorageSync('cid', cid);
			});

			that.handlePush()
			
			var brand = uni.getSystemInfoSync().deviceBrand
			setRequestPermissionTips(PermissionTips)
			registerRequestPermissionTipsListener({
				
				onRequest: (e) => {
					console.log(e)
					
				},
				onConfirm: (e) => {
					console.log(e)
				},
				onComplete: (e) => {
					// 华为手机在权限禁止之后，再次申请权限不会出现权限申请框。此时应该引导用户去系统设置开启此权限，不应该频繁申请。
					if (brand.toLowerCase() == "huawei") {
						var tips = {}
						var hasDeniedPermission = false
						for (var k in PermissionTips) {
							if (e[k] != "denied") {
								tips[k] = PermissionTips[k]
							} else {
								hasDeniedPermission = true
							}
						}
						setRequestPermissionTips(tips) // 更新弹框提醒，防止华为手机不出现权限申请框时权限提醒框闪烁的情况
						if (hasDeniedPermission)
							uni.showModal({
								content: "权限已经被拒绝，请前往设置中开启"
							})
					}
				}
			})
			
			// #endif

			

		},
		onShow: function() {
			this.isHaveNetwork()
			// console.log('App Show')
		},
		onHide: function() {
			// console.log('App Hide')
		},
		onExit: function() {
			unregisterRequestPermissionTipsListener(null)
		},
		onLoad() {

		},
		methods: {

			// 处理推送消息
			handlePush() {
				// #ifdef APP-PLUS
				const _receivePush = function(message) {
					// 获取推送的信息  判断是不是ios
					if (uni.getSystemInfoSync().platform == 'ios') {
						let {
							title,
							content
						} = message;
						plus.push.createMessage(content, title, {
							title: title,
							// icon: '@/static/images/me/no-head-photo.png'
						});
					}
				}
				// 进入到app中某个页面
				const _clickPush = function(message) {
					console.log(message)
					// payload(JSON) 可携带自定义信息用于通过路由传参
					let {
						payload
					} = message;
					if (payload.content == '有新的运费订单了!') {
						uni.switchTab({
							url: '/pages/tabbar/receivingOrder/receivingOrder'
						})
					}
					if (payload.content == '您已成功完成当日首单,已获得2次抽奖机会!') {
						uni.navigateTo({
							url: '/pages/zpcjlist/index'
						})
					}
					if (payload.content == '您邀请的用户已注册成功!') {
						uni.switchTab({
							url: '/pages/tabbar/home/index'
						})
					}
					if (payload.content == '您邀请的用户已成功完成一单,您的收益增加10元!') {
						uni.switchTab({
							url: '/pages/tabbar/main/index'
						})
					} else {
						uni.navigateTo({
							url: '/pages/masgList/index'
						})
					}

				}
				// 用户点击消息栏上的消息通知
				plus.push.addEventListener('click', _clickPush)
				// 收到推送的消息 ios设备app在线的时候需要创建一个消息通知
				plus.push.addEventListener('receive', _receivePush)
				// #endif
			},

			isHaveNetwork() {
				uni.getNetworkType({
					success: function(res) {
						if (res.networkType == 'none') {
							uni.showModal({
								title: '网络链接失败',
								content: '检测到网络权限可能设置为关闭，您可以在“设置”中检查无线数据及蜂窝移动网络'
							})
						}
					}
				})
			},

			init() {
				if (uni.getStorageSync('userinfo')) {
					var userinfo = JSON.parse(uni.getStorageSync('userinfo'))
					console.log(userinfo)
					if (userinfo.PERTB005ID != undefined) {
						// console.log('ErpUserType'+this.ErpUserType)
						// if (this.ErpUserType == 'HF') {

						// }
						uni.switchTab({
							url: '/pages/tabbar/home/index'
						});
					}
				}
			},

			isDate() { //检测是有新版本
				var that = this

				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					uni.request({
						url: that.baseUrl + '/set/settb96/settb96ById',
						dataType: 'json',
						method: 'POST',
						data: {
							SETTB96CA01: widgetInfo.version,
							SETTB96CA02: 1001
						},
						success: (result) => {
							console.log(result)
							if (result.data.code == 200) {
								if (result.data.data.SETTB96CD02 == 0 && result.data.data
									.SETTB96CD03 == 0) {
									uni.reLaunch({
										url: '/pages/mainUpdate/index'
									});
								}
								if (result.data.data.SETTB96CD02 == 1 && result.data.data
									.SETTB96CD03 == 0) {
									uni.reLaunch({
										url: '/pages/appDown/index'
									});
								}
								if (result.data.data.SETTB96CD02 == 1 && result.data.data
									.SETTB96CD03 == 1) {
									that.zygx = result.data.data.SETTB96CD03
									that.iskeppLogin()

									uni.getSystemInfo({
										success: (res) => {
											//检测当前平台，如果是安卓则启动安卓更新  
											if (res.platform == "android") {
												uni.showModal({ //提醒用户更新  
													title: "更新提示",
													content: '有新的版本是否更新',
													success: (res) => {
														if (res.confirm) {
															// that.showdownLine =
															// 	true
															// that.doUpData(
															// 	'http://www.feishazoushi.cn/download/customer.apk'
															// )
															uni.reLaunch({
																url: '/pages/appDown/index'
															})


															// plus.runtime.openURL(androidUrl)

														} else {
															console.log(
																'取消'
															) //点击取消之后执行的代码
														}
													}
												})
											}
										}
									})




									// uni.showModal({ //提醒用户更新
									// 	title: '提示',
									// 	content: '是否进行更新',
									// 	success: function(res) {
									// 		if (res.confirm) {
									// 			plus.runtime.openURL(
									// 				'http://www.feishazoushi.cn/download/customer.apk'
									// 			); //进入浏览器下载
									// 		} else {
									// 			console.log('取消')
									// 		}
									// 	}
									// })

								}
							} else {
								that.iskeppLogin()
							}
						}
					});
				});
			},
			iskeppLogin() {
				var tokenStr = 'token'
				if (uni.getStorageSync('token')) {
					tokenStr = uni.getStorageSync('token')
				}
				console.log(uni.getStorageSync('token'))
				uni.request({
					url: this.baseUrl + '/system/keeplogin/' + tokenStr, //仅为示例，并非真实接口地址。
					method: 'GET',
					success: (res) => {
						console.log(res)
						if (res.data.code == 200) {
							// if (this.iskeepIndex == 1) {

							// }
							this.init()
						}
						if (res.data.code == 201) {
							uni.clearStorageSync()
							uni.reLaunch({
								url: '/pages/APPlogin/index'
							})

							return
							// uni.showModal({
							// 	title: '提示',
							// 	content: res.data.message,
							// 	showCancel: false,
							// 	success:(res) => {
							// 		if (res.confirm) {


							// 		} else if (res.cancel) {
							// 			console.log('用户点击取消');
							// 		}
							// 	},
							// 	complete: () => {
							// 	}
							// });




						}



						// else {
						// 	if (tokenStr != 'token') {
						// 		uni.reLaunch({
						// 			url: '/pages/APPlogin/index'
						// 		})
						// 		uni.clearStorageSync()
						// 	}
						// }
						// setTimeout(() => {
						// 	this.iskeepIndex++
						// 	this.iskeppLogin()
						// }, 5000);
					},
					fail: (res) => {
						uni.showToast({
							title: '服务器连接超时',
							duration: 5000,
							icon: 'none',
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import url("./static/font/iconfont.css");
	@import "uview-ui/index.scss";

	/*去掉横拉条*/
	::-webkit-scrollbar {
		width: 0;
		height: 0;
		// background-color: #fff;
	}

	// 数据异常图片
	.public-no-img {
		width: 45vw;
		height: 450rpx;
		margin: 0 auto 0;
		padding-top: 100rpx;
	}

	.public-no-img image {
		width: 100%;
		height: 100%;
	}

	.notice .u-icon__icon {
		color: #ff9900 !important;
	}

	.public-nav-fixed {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 999;
		background-color: #FFF;
	}

	.public-nav-top-navbar-inner {
		position: relative;
	}

	.public-nav-top-navbar-inner text {
		position: absolute;
		top: 0;
		left: 0;
		width: 100rpx;
		text-align: center;
		font-size: 34rpx;
		z-index: 99;
	}

	.public-nav-top-navbar-inner view {
		flex: 1;
		text-align: center;
		font-size: 34rpx;
		font-weight: 600;
	}

	.public-nav-option {
		height: inherit;
		display: flex;
		font-size: 30rpx;
		color: #333;
		text-align: center;
		overflow: hidden;
	}

	.public-nav-top-navbar-inner .iconarrow_left-copy {
		font-weight: 600;
	}

	// 客户管理
	.customerManagement .u-navbar-content-title,
	.customerManagement .u-title {
		height: 0 !important;
	}

	// 线上充值
	.OnlineRecharge .OnlineRecharge-radio-group {
		width: 100%;
	}

	.OnlineRecharge .u-radio__icon-wrap--circle {
		position: absolute;
		top: 27%;
		left: 90%;
	}

	.OnlineRecharge .u-radio {
		border-bottom: 2rpx solid rgba(230, 230, 230, 1);
		padding: 25rpx 0;
		position: relative;
		top: 0;
		left: 0;
	}
</style>